package com.itsm.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itsm.entity.Process;
import com.itsm.entity.Process1;
import com.itsm.service.mapper.ProcessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

//@DS("master")
@Service
public class ProcessAllService {

    @Autowired
    private ProcessMapper processMapper;


    public List<Process> selectProcess(Integer state){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("state",state);
        return processMapper.selectList(wrapper);
    }

    /**
     * 查询转派的单是否被接受了
     * @param state 工单状态
     * @param is 是否被接受
     * @return 工单信息
     */
    public List<Process> selectProcessAcc(Integer state,Integer is){
        QueryWrapper<Process> wrapper = new QueryWrapper();
        Map<String, Object> queryParamsMap = new HashMap<>();
        queryParamsMap.put("state", state);
        queryParamsMap.put("is_accepted", is);
        queryParamsMap.put("is_delete", 1);
        wrapper.allEq(queryParamsMap).orderByDesc("process_no");
        return processMapper.selectList(wrapper);
    }


    public List<Process> selectRateNo(Integer state, Integer is){
        QueryWrapper<Process> wrapper = new QueryWrapper();
        Map<String, Object> queryParamsMap = new HashMap<>();
        queryParamsMap.put("state", state);
        queryParamsMap.put("is_accepted", is);
        queryParamsMap.put("is_delete", 1);
        wrapper.allEq(queryParamsMap).orderByDesc("process_no");
        wrapper.lambda().isNull(Process::getScore);
        return processMapper.selectList(wrapper);
    }

    public List<Process> selectRate(Integer state, Integer is){
        QueryWrapper<Process> wrapper = new QueryWrapper();
        Map<String, Object> queryParamsMap = new HashMap<>();
        queryParamsMap.put("state", state);
        queryParamsMap.put("is_accepted", is);
        queryParamsMap.put("is_delete", 1);
        wrapper.allEq(queryParamsMap).orderByDesc("process_no");
        wrapper.lambda().isNotNull(Process::getScore);
        return processMapper.selectList(wrapper);
    }

    /**
     * 查询各状态的工单
     * @return
     */
    public List<Process> selectProcessAcc1(Integer state,Integer is,Integer plant){

        return processMapper.get1(state,is,plant);
    }


    /**
     * 未派单的工单
     * @param plant 工厂区分
     * @param all 所有
     * @return 未派单的工单
     */
    public List<Process> UnDispatch(Integer plant, Integer all){
        if(all!=0){
            return processMapper.getUnDispatch3();
        }else {
            return processMapper.getUnDispatch2(plant);
        }
    }

    /**
     * 未审批工单查询
     * @return 未审批工单查询
     */
    public List<Process1> selectListUnApp(Integer plant, Integer all){
        if(all!=0){
            return processMapper.getAllUnApp1();
        }else {
            return processMapper.getAllUnApp(plant);
        }
    }

    /**
     * 按照工单是否需要审批，查询区分的全部工单
     * @return 区分的全部工单
     */
    public List<Process> selectListAssign(Integer plant,Integer all){
        if(all!=0){
            return processMapper.getAll();
        }else {
            return processMapper.getAllNo(plant);
        }
    }


    /**
     * 按照工单是否需要审批，查询区分的全部工单
     * @return 区分的全部工单
     */
    public List<Process1> selectListAssign1(Integer plant, Integer all){
        if(all!=0){
            return processMapper.getAll1();
        }else {
            return processMapper.getAllNo1(plant);
        }
    }


    /**
     * 查询工单（服务台）
     * @return 查询的工单
     */
    public List<Process1> selectListAssign2(Integer plant, Integer all,String userName){
        if(all!=0){
            return processMapper.getAll2(userName);
        }else {
            return processMapper.getAllNo2(plant,userName);
        }
    }

    /**
     * 更新所转派的工程师
     * @param process
     * @return
     */
    public Integer updateEn(Process process){

        return processMapper.updateById(process);
    }



    public Process select(String processNo){

        return processMapper.selectById(processNo);
    }






}
